package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.ordervo.CourseWebVoOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.atguigu.handler.GuiliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.file.OpenOption;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-03
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private OrderService orderService;

    @Override

    public String createOrders(String courseId, String memberId) {

        Optional<Order> optional = Optional.ofNullable(orderService.getOne(new QueryWrapper<Order>().eq("course_id", courseId)));


        if (!optional.isPresent()){
            //通过远程调用根据用户id获取用户信息
            Optional<UcenterMemberOrder> optional1 = Optional.ofNullable(ucenterClient.getUserInfoOrder(memberId));


            if (!optional1.isPresent()){
                throw new GuiliException(20001,"请先登录撒");
            }
            UcenterMemberOrder userInfoOrder = ucenterClient.getUserInfoOrder(memberId);

            //通过远程调用根据课程id获取课信息
            CourseWebVoOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

            //创建Order对象，向order对象里面设置需要数据
            Order order = new Order();
            order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
            order.setCourseId(courseId); //课程id
            order.setCourseTitle(courseInfoOrder.getTitle());
            order.setCourseCover(courseInfoOrder.getCover());
            order.setTeacherName(courseInfoOrder.getTeacherName());
            order.setTotalFee(courseInfoOrder.getPrice());
            order.setMemberId(memberId);
            order.setMobile(userInfoOrder.getMobile());
            order.setNickname(userInfoOrder.getNickname());
            order.setStatus(0);  //订单状态（0：未支付 1：已支付）
            order.setPayType(1);  //支付类型 ，微信1
            baseMapper.insert(order);
            //返回订单号
            return order.getOrderNo();
        }

        return orderService.getOne(new QueryWrapper<Order>().eq("course_id", courseId)).getOrderNo();

    }

    @Override
    public Boolean isBoughtByCourseId(String courseId, String jwtToken) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("member_id", jwtToken)
                .eq("course_id", courseId)
                .eq("status", 1);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count.intValue() > 0;
    }

    @Override
    public List<Order> selectByMemberId(String jwtToken) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .orderByDesc("gmt_create")
                .eq("member_id", jwtToken);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean removeById(String orderId, String jwtInfo) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("id", orderId)
                .eq("member_id", jwtInfo);
        return this.remove(queryWrapper);
    }

    @Override
    public int getonestatus(String courseId,String memberId) {
        int count = baseMapper.getstatus(courseId, memberId);
        return count;
    }

}
